package org.inspiration.dudu.web.service.content.imp;

import lombok.RequiredArgsConstructor;
import org.inspiration.dudu.common.util.FileUtil;
import org.inspiration.dudu.content.model.dto.AnswerDTO;
import org.inspiration.dudu.content.model.entity.AnswerEntity;
import org.inspiration.dudu.content.model.vo.AnswerVO;
import org.inspiration.dudu.content.service.AnswerService;
import org.inspiration.dudu.content.service.QuestionService;
import org.inspiration.dudu.user.service.UserService;
import org.inspiration.dudu.web.service.content.AnswerFacadeService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;

@Service
@RequiredArgsConstructor
public class AnswerFacadeServiceImpl implements AnswerFacadeService {

    @Value("${dudu.file.upload-path}")
    private String uploadPath;

    private final FileUtil fileUtil;
    private final AnswerService answerService;
    private final QuestionService questionService;
    private final UserService userService;

    /**
     * 上传回答
     * 
     * @param answerDTO 包含回答内容的DTO对象
     * @return 返回生成的UUID
     */
    @Override
    @Transactional
    public String uploadAnswer(AnswerDTO answerDTO) {
        String uuid = UUID.randomUUID().toString();
        String filename = uuid + ".html";
        Path subPath = Paths.get("answers", filename);
        // 将内容存入文件
        fileUtil.upload(answerDTO.getContent(), subPath);
        // 将实体存入数据库
        String contentFilePath = Paths.get(uploadPath).resolve(subPath).toString();
        AnswerEntity answerEntity = answerService.transToEntity(answerDTO, contentFilePath, uuid);
        answerService.createAnswer(answerEntity);

        // 增加问题的回答数量
        questionService.increaseAnswerCount(answerDTO.getParentId());
        // 增加发布者的回答数量
        userService.increaseAnswerCount(answerDTO.getUserId());

        return uuid;
    }

    /**
     * 根据问题ID按升序获取所有回答
     * 
     * @param questionId 问题ID
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @return 返回回答列表
     */
    @Override
    public List<AnswerVO> getAnswersByQuestionIdOrderByASC(String questionId, Integer pageNum, Integer pageSize) {
        return answerService.getAnswersByQuestionIdOrderByASC(questionId, pageNum, pageSize);
    }

    /**
     * 根据问题ID按降序获取所有回答
     * 
     * @param questionId 问题ID
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @return 返回回答列表
     */
    @Override
    public List<AnswerVO> getAnswersByQuestionIdOrderByDESC(String questionId, Integer pageNum, Integer pageSize) {
        return answerService.getAnswersByQuestionIdOrderByDESC(questionId, pageNum, pageSize);
    }

    /**
     * 获取单个回答
     * 
     * @param id 回答ID
     * @return 返回回答VO对象
     */
    @Override
    public AnswerVO getAnswerById(String id) {
        return answerService.getAnswerById(id);
    }

    @Override
    public List<Map<String, Object>> getAnswersByUserIdOrderByDESC(Long userId, Integer pageNum, Integer pageSize) {
        List<Map<String, Object>> res =new ArrayList<>();
        //先获取所有的问题，再根据每个问题的父id获取对应的问题
        List<AnswerVO> answerList = answerService.getAnswersByUserIdOrderByDESC(userId, pageNum, pageSize);
        for(AnswerVO answerVO : answerList) {
            Map<String, Object> map = new HashMap<>();
            map.put("answer", answerVO);
            map.put("question", questionService.getQuestionById(answerVO.getParentId()));
            res.add(map);
        }
        return res;
    }
}
